/**
 * 登陆窗口
 */
define(function(){
	var LoginWin = Ext.extend(Ext.Window, {
	    title: '登陆窗口',
	    width: 350,
	    height: 250,
	    layout: 'anchor',
	    plain:true,
	    modal: false,
	    resizable: false,
	    closable: false,
	    draggable: true,
		collapsible: true,
	    bodyStyle:'padding:5px;',
	    buttonAlign:'right',
	    iconCls: 'icon-window',
	    animateTarget: cu.getDefaultAnimateTarget(),
		initComponent: function(){
			this.items = [
		        this.createBannerPanel(100),
		    	this.formPanel = this.createLoginForm()
		    ];
			this.buttons = [
		        {text: '登陆', handler: this.login, iconCls: 'icon-login', scope: this }
		        //{text: '重置', handler: function(){this.getForm().reset();},iconCls: 'icon-modify', scope: this}
		    ];
			LoginWin.superclass.initComponent.call(this);
			this.on('show', function(){
				this.firstFocus();
				cu.bindEnter(this, this.login);
			});
		},
		//自定义属性和方法
		login: function(){
			cu.submit({
				form: this.getForm(),
				url: 'sys/user/login.do',
				success: function(data){
					au.setCookie('lastLoginUser', this.getForm().findField('code').getValue()); //保存cookie
					this.close();
			    	if (this.callback){
			    		this.callback.call(this.cbScope, data);
			    	}
				},
				failure: function(msg){
					cu.alert(msg, function(){
						this.getForm().findField('password').focus();
					}, this);
				},
				scope: this
			});
		},
		getForm: function(){
			return this.formPanel.getForm();
		},
		firstFocus: function(){
			var userCode = this.getForm().findField('code').getValue();
			this.getForm().items.itemAt(userCode ? 1 : 0).focus(true, 200);
		},
		createLoginForm: function(){
			return new Ext.form.FormPanel({
			    baseCls: 'x-plain',
			    style: 'margin-top:10px;',
			    labelWidth: 50,
			    labelPad: 5,
			    labelSeparator: ':',
			    labelAlign: "right",
			    defaultType: 'textfield',
			    items: [{
			        fieldLabel: '账号',
			        name: 'code',
			        value: au.getCookie('lastLoginUser'),
			        allowBlank:false,
			        anchor:'99%'
			    },{
			    	inputType: 'password',
			        fieldLabel: '密码',
			        name: 'password',
			        allowBlank:false,
			        selectOnFocus: true,
			        anchor: '99%'
			    }]
			});
		},
		createBannerPanel: function(height){
			var width = Ext.isIE6 ? '305px' : '100%';
			return new Ext.Panel({
				height: height || 100,
				html: '<div style="width:{0};height:100%;background:url(theme/default/images/login_banner.png);\
					padding:20px 0 0 20px;">欢迎使用{1}</div>'.f(width, au.getSysParam('app.title'))
			});
		}
	});
	return LoginWin;
});